home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / Drag_on Modules / hAWK programs / $TabsToSpaces < prev    next >
Text File  |  1993-04-09  |  4KB  |  124 lines

  1. #$TabsToSpaces: converts tabs to spaces in one or more documents,
  2. #replacing each tab by the appropriate number of spaces (anywhere
  3. #from 1 to “spaces_in_tabs”), consistent with the tab interpretation
  4. #of THINK C et al.
  5. #Use “Set variables” in the setup dialog to set the following:
  6. #Variable=default        meaning
  7. #----------------        ----------------
  8. #    spaces_in_tab=4        the number of spaces per tab in your documents
  9. #    overwrite=0            uses the function “MakeNewFileName()” to
  10. #                        produce a name for the copy of the file
  11. #                        (if =1, overwrites the
  12. #                        original file).
  13. #
  14. #Run on a specific file or on MFS selected files. Overwrites original
  15. #file if overwrite=1, so MAKE A COPY FIRST if you want to keep original.
  16. #Note if you would prefer to make a copy of the file rather than
  17. #overwriting the original, leave “overwrite=0”; this invokes the
  18. #function MakeNewFileName() which appends “s” (for space) to the
  19. #file name, eg turning "main.c" into "main.cs". You can change this
  20. #function to produce different names for the copies, even
  21. #autoincrementing a version number with something like:
  22. #    -- split FILENAME into names[] as in MakeNewFileName()
  23. #    # assuming names look like "ProperNameNNN.c" where NNN = version number
  24. #    if (match(names[z], /[0-9]+\./)) #digits followed by period
  25. #        {
  26. #        num = substr(names[z], RSTART, RLENGTH-1) #skip period
  27. #        ++num #hAWK string/number power at work
  28. #        sub(/[0-9]+\./, num ".", names[z]) #remember to put back the matched period
  29. #        }
  30. #    else if (match(names[z], /\./)) #no version number yet
  31. #        sub(/\./. "1.", names[z])
  32. #    else #give up, tack on an s.
  33. #        names[z] = names[z] "s"
  34. #    -- the rest as in MakeNewFileName()
  35.  
  36. # User’s Manual references:
  37. # «hAWK User’s Manual» «F   Running hAWK programs»
  38. # «hAWK User’s Manual» «L  5   Regular expressions»
  39. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  40. # «hAWK User’s Manual» «K  4   Built-in variables»
  41. # «hAWK User’s Manual» «K  8   Arrays»
  42. # «hAWK User’s Manual» «N   User-defined functions»
  43. # «hAWK User’s Manual» «P  3   The getline function»
  44. # «hAWK User’s Manual» «O  3   Output into files»
  45. # «hAWK User’s Manual» «Q   The hAWK function»
  46.  
  47. BEGIN {
  48.     #For position of tab from 1 to 200 or so, precompute number of
  49.     #spaces required to replace tab at that position.
  50.     for (i = 0; i <= 200; ++i)
  51.         n_first_tab[i+1] = (int((i)/spaces_in_tab)+1)*spaces_in_tab - i;
  52.     #For speed, set up array of spaces.
  53.     spaces[1] = " "
  54.     for (i = 2; i <= 100; ++i)
  55.         spaces[i] = spaces[i-1] " ";
  56.     n = 0#not necessary, but helps reading
  57.     if (overwrite+0 != 0)
  58.         overwrite = 1
  59.     else
  60.         overwrite = 0
  61.     }
  62.  
  63. FNR == 1{ #at the first line of a file...
  64.     #Flush buffer if overwriting file.
  65.     if (n > 0)
  66.         {
  67.         for (i = 1; i <= n; ++i)
  68.             {
  69.             print out[i] > outfile;
  70.             delete out[i];
  71.             }
  72.         close(outfile);
  73.         n = 0;
  74.         }
  75.     else if (outfile != "")
  76.         close(outfile);
  77.     #Standard action is to overwrite the input file with the altered version.
  78.     if (overwrite)
  79.         outfile = FILENAME;
  80.     else
  81.         outfile = MakeNewFileName()
  82.     }
  83.  
  84. #The main event: rep tabs with spaces, and print immediately if copying file.
  85. #Buffer altered lines to the out[] array if overwriting.
  86. #Note if hAWK gives up due to out-of-memory while buffering up, then
  87. #the orginal file will not be touched. The “delete out[i]” above
  88. #frees up memory for each new file. 
  89.     { # No action implies this pattern executed for all input lines...
  90.     while (match($0, /\t+/)) # sets RSTART, RLENGTH
  91.         {
  92.         #First tab in group counts for a variable number of spaces.
  93.         total_sp = n_first_tab[RSTART] + spaces_in_tab *(RLENGTH-1)
  94.         sub(/\t+/, spaces[total_sp]) #replaces leftmost group only
  95.         }
  96.     if (overwrite)
  97.         out[++n] = $0
  98.     else
  99.         print $0 > outfile
  100.     }
  101.  
  102. END {
  103.     if (n > 0)
  104.         {
  105.         for (i = 1; i <= n; ++i)
  106.             {
  107.             print out[i] > outfile;
  108.             }
  109.         close(outfile);
  110.         }
  111.     }
  112.  
  113. function MakeNewFileName(    z, i, outfile)
  114.     {
  115.     z = split(FILENAME, names, ":");
  116.     names[z] = names[z] "s"
  117.     if (length(names[z]) > 31)
  118.         names[z] = substr(names[z], 2)#trim first letter - not elegant...
  119.     outfile = names[z]
  120.     for (i = z-1; i >= 1; --i) #put full path name back together
  121.         outfile = names[i] ":" outfile;
  122.     return outfile
  123.     }
  124.